const Koa=require('koa');
const app=new Koa();

const Router=require('koa-router');

const home=new Router();
//子路由1
home.get('/',ctx=>{
    ctx.body=`<ul>
        <li><a href="/posts">/posts</a></li>
    </ul>`;
});


//子路由2
const posts=new Router();
posts.get('/',ctx=>{
    ctx.body='posts!';
});

//装载路由
const router=new Router();
router.use('/',home.routes(),home.allowedMethods());
router.use('/posts',posts.routes(),posts.allowedMethods());

//加载路由中间件
app.use(router.routes()).use(router.allowedMethods());

app.listen(4000,()=>{
    console.log('Koa2 port:4000----'+new Date().toLocaleTimeString());
});